package com.zjsru.plan2024.oneday;

/**
 * 633. 平方数之和
 *
 * @Author: cookLee
 * @Date: 2024-11-04
 */
public class JudgeSquareSum {
    /**
     * 主
     * \
     * 输入：c = 5
     * 输出：true
     * 解释：1 * 1 + 2 * 2 = 5
     * \
     * 输入：c = 3
     * 输出：false
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        JudgeSquareSum judgeSquareSum = new JudgeSquareSum();
        int c = 8;
        System.out.println(judgeSquareSum.judgeSquareSum2(c));

    }

    /**
     * 判断平方和
     *
     * @param c c
     * @return boolean
     */
    public boolean judgeSquareSum(int c) {
        for (int i = 0; i * i <= c; i++) {
            int b = c - i * i;
            if (Math.sqrt(b) % 1 == 0) {
                return true;
            }
        }
        return false;
    }

    public boolean judgeSquareSum2(int c) {
        for (int i = 0; i * i <= c; i++) {
            double b = Math.sqrt(c - i * i);
            if ((int) b == b) {
                return true;
            }
        }
        return false;
    }

    /**
     * 双指针
     *
     * @param c c
     * @return boolean
     */
    public boolean judgeSquareSum3(int c) {
        long left = 0;
        long right = (long) Math.sqrt(c);
        while (left <= right) {
            long sum = left * left + right * right;
            if (sum == c) {
                return true;
            } else if (sum > c) {
                right--;
            } else {
                left++;
            }
        }
        return false;
    }

}
